package com.sta.oldman.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sta.oldman.common.ResponseVO;
import com.sta.oldman.dao.Exception;
import com.sta.oldman.dao.User;
import com.sta.oldman.dao.mapper.ExceptionMapper;
import com.sta.oldman.dao.mapper.UserMapper;
import com.sta.oldman.enums.CheckUserEnum;
import com.sta.oldman.enums.ExceptionEnum;
import com.sta.oldman.model.req.BindEmailReq;
import com.sta.oldman.model.req.ExceptionReq;
import com.sta.oldman.service.ExceptionService;
import com.sta.oldman.utils.QQEmailUtils;
import com.sta.oldman.utils.SnowFlakeUtil;
import com.sta.oldman.utils.VerificationCodeService;
import com.sta.oldman.utils.VerificationCodeUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Locale;

/**
 * @author: LunBoWang
 * @description: TODO
 * @date: 2025/7/21 下午7:37
 * @version: 1.0
 */
@Service
public class ExceptionServiceImpl implements ExceptionService {

    @Resource
    UserMapper userMapper;
    @Resource
    ExceptionMapper exceptionMapper;
    @Resource
    private VerificationCodeService verificationCodeService;

    @Override
    public ResponseVO getException(ExceptionReq req) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("imei", req.getImei());
        List<User> users = userMapper.selectList(queryWrapper);
        if (users == null) {
            return ResponseVO.errorResponse(CheckUserEnum.USER_NOT_EXIST.getCode(), CheckUserEnum.USER_NOT_EXIST.getMessage());
        }
       for (User user:users){
           String imei=user.getImei();
           String content= ExceptionEnum.getDesByCode(req.getExceptionType());
           Exception exception = new Exception();
           exception.setImei(imei);
           exception.setExceptionContent(content);
           int update = exceptionMapper.insert(exception);
           if(update==0){
               return ResponseVO.errorResponse();
           }
           VerificationCodeUtils.CodeWithExpiration codeWithExpiration = VerificationCodeUtils.generateCodeWithExpiration();
           verificationCodeService.saveCode(user.getEmail(),codeWithExpiration.getCode());
           // 获取当前时间
           LocalDateTime now = LocalDateTime.now();

           // 定义中文格式
           DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
                   "yyyy年M月d日H时m分",
                   Locale.CHINA
           );

           // 格式化时间
           String formattedDateTime = now.format(formatter);
           String verificationCodeHtml = VerificationCodeUtils.createAlertHtml(content,formattedDateTime);
           QQEmailUtils.sendHtmlEmail(user.getEmail(), "异常警报",verificationCodeHtml);
       }
        return ResponseVO.successResponse("报警成功");
    }

    @Override
    public ResponseVO bindingEmail(BindEmailReq req) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email", req.getEmail());
        User user = userMapper.selectOne(queryWrapper);
        if (user != null) {
           return ResponseVO.errorResponse(CheckUserEnum.EMAIL_EXIST.getCode(), CheckUserEnum.EMAIL_EXIST.getMessage());
        }
        user = new User();
        user.setEmail(req.getEmail());
        user.setUserId(SnowFlakeUtil.getSnowFlakeId());
        user.setImei("1");
        user.setUsername(SnowFlakeUtil.getSnowFlakeId().toString().substring(0,10));
        user.setPassword("123456");
        int insert = userMapper.insert(user);
        if(insert==0){
            return ResponseVO.errorResponse();
        }
        return ResponseVO.successResponse("绑定成功");
    }
}
